// pages/cate/cate.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid:'',
    classifyleft:[],
    classifyright:[],
    tabActive:0
  },
  bindmenu:function(e){
    let index=e.currentTarget.dataset.index;
    let cid=this.data.classifyleft[index].cid;
    this.setData({
      tabActive:index
    })
    this.getSecondClassify(cid);
  },
  getClassify:function(){//一级
    let data={cid:'',isajax:0};
    let cateId=app.globalData.cateId;
    app.requestJs('Goods/cateindex',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        let index=cateId==''?0:resData.findIndex(item=>item.cid==cateId);
        console.log(index);
        console.log(resData);
        this.setData({
          classifyleft:resData,
          tabActive:index
        })
        let cid=resData.length<=index?'':resData[index].cid;
        if(cid=='')return;
        else this.getSecondClassify(cid,index);
      }else app.utilJs.toast({ title: res.message });
    })
  },
  getSecondClassify:function(cid){
    let data={cid:cid || this.data.cid,isajax:1};
    app.requestJs('Goods/cateindex',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        console.log(resData);
        this.setData({
          classifyright:resData,
          cid:data.cid
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.cateId);
    let cateId=app.globalData.cateId,
        classifyleft=this.data.classifyleft;
    if(classifyleft.length==0 || cateId=='')this.getClassify();
    else{
      let index=classifyleft.findIndex(item=>item.cid==cateId);
      console.log(index);
      let cid=index==-1?classifyleft[0].cid:classifyleft[index].cid;
      this.setData({
        tabActive:index==-1?0:index
      })
      this.getSecondClassify(cid);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})